Clean-up partition-columns implementation
authorjustbur <justin@burkett.cc>
Mon, 3 Aug 2015 15:26:56 +0000 (11:26 -0400)
committerjustbur <justin@burkett.cc>
Mon, 3 Aug 2015 16:39:15 +0000 (12:39 -0400)
which-key.el

index f992a5435d8186b9f19631f688c3bc4e31434cf4..321baf0a95d3af08881895ed71b9d2c0ee5a9e3e 100644 (file)
@@ -921,32 +921,28 @@ Returns a plist that holds the page strings, as well as metadata."
                                (-partition-all avl-lines keys)))
         (page-width 0) (n-pages 0) (n-keys 0)
         page-cols pages page-widths keys/page)
-    (if (> (car (car cols-w-widths)) avl-width)
-        ;; give up if first column doesn't fit
+    (if (> (apply #'max (mapcar #'car cols-w-widths)) avl-width)
+        ;; give up if any columns don't fit
         (list :pages nil :page-height 0 :page-widths '(0)
               :keys/page '(0) :n-pages 0 :tot-keys 0)
-      (dolist (col cols-w-widths)
-        (if (<= (+ (car col) page-width) avl-width)
-            (progn (push (cdr col) page-cols)
-                   (setq page-width (+ page-width (car col))
-                         n-keys  (+ (length (cdr col)) n-keys)))
-          (when (> (length page-cols) 0)
-            (push (which-key--join-columns page-cols) pages)
-            (push n-keys keys/page)
-            (push page-width page-widths)
-            (setq n-pages (1+ n-pages)
-                  n-keys (length (cdr col))
-                  page-cols (list (cdr col))
-                  page-width (car col)))))
-      (when (> (length page-cols) 0)
+      (while cols-w-widths
+        (when (not (<= (+ (caar cols-w-widths) page-width) avl-width))
+          (error "which-key: error in partition-columns"))
+        (while (and cols-w-widths
+                    (<= (+ (caar cols-w-widths) page-width) avl-width))
+          (setq col (pop cols-w-widths)
+                page-width (+ page-width (car col))
+                n-keys  (+ (length (cdr col)) n-keys))
+          (push (cdr col) page-cols))
         (push (which-key--join-columns page-cols) pages)
         (push n-keys keys/page)
         (push page-width page-widths)
-        (setq n-pages (1+ n-pages)))
+        (setq n-pages (1+ n-pages)
+              n-keys 0 page-cols '() page-width 0))
       (list :pages (reverse pages) :page-height avl-lines
             :page-widths (reverse page-widths)
             :keys/page (reverse keys/page) :n-pages n-pages
-            :tot-keys (cl-reduce '+ keys/page :initial-value 0)))))
+            :tot-keys (apply #'+ keys/page)))))
 
 (defun which-key--create-pages (keys sel-win-width)
   "Create page strings using `which-key--partition-columns'.